package com.wawei.seeds.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.wawei.seeds.R;
import com.wawei.seeds.model.UserImages;
import com.wawei.seeds.utils.ImageLoader;

import java.util.List;

/**
 * Created by itachi on 15/7/17.
 */
public class CommentGridAdapter extends BaseAdapter {
    private List<UserImages> mUI;
    private ImageLoader imageLoader;

    private LayoutInflater mLayoutInflater;

    public CommentGridAdapter(List<UserImages> ui, Context context) {
        mLayoutInflater = LayoutInflater.from(context);
        this.mUI = ui;
        this.imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return mUI == null ? 0 : mUI.size();
    }

    @Override
    public String getItem(int position) {
        return mUI.get(position).urls;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommentGridViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new CommentGridViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.user_img_item,
                    parent, false);
            viewHolder.imageView = (ImageView) convertView
                    .findViewById(R.id.iv_user_img);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (CommentGridViewHolder) convertView.getTag();
        }
        String url = getItem(position);
        imageLoader.loadImage(url, viewHolder.imageView, true, false);
        return convertView;
    }

    private static class CommentGridViewHolder {
        ImageView imageView;
    }
}
